﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Text;
using NabNetLib.Remoting.Interface;
using System.Runtime.Remoting.Channels.Tcp;

namespace NabNetLib.Remoting
{
    public class RemoteNabNet : MarshalByRefObject, IRemoteNabNet
    {
        public override object InitializeLifetimeService()
        {
            //no limit lifetime object
            return null;
        }

        #region IRemoteNabNet Members

        public void RfidAction(string lapinSerialNumber, string rfidSerialNumber)
        {
            XmppListener.SendRfidMessage(lapinSerialNumber,rfidSerialNumber);
        }

        public void RecordAction(string lapinSerialNumber, string wavPath)
        {
            XmppListener.SendRecordMessage(lapinSerialNumber,wavPath);
        }

        #endregion

        public static void RunRemotingServer()
        {
            try
            {
                // Création d'un nouveau canal d'écoute sur le port 1069
                TcpChannel channel = new TcpChannel(1069);
                // Enregistrement du canal
                ChannelServices.RegisterChannel(channel,false);
                // Démarrage de l'écoute en exposant l'objet en Singleton

                RemotingConfiguration.RegisterWellKnownServiceType(
                    typeof(RemoteNabNet),
                    "RemoteNabNet",
                    WellKnownObjectMode.Singleton);
            }
            catch (Exception ex)
            {
                // starting server fail
                Logger.Log("Error during RunRemotingServer", ex);
            }
        }
    }
}
